package com.zmzncs.shop.module.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.shop.common.pojo.PageInfo;
import com.zmzncs.shop.common.pojo.Rest;
import com.zmzncs.shop.common.util.JsonUtil;
import com.zmzncs.shop.common.util.RestUtil;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shopgoods.*;
import org.springframework.web.bind.annotation.*;
import com.zmzncs.shop.module.shop.service.ShopGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RestController;

/**
 * @author 高博文
 * @since 2021-05-25
 */
@Api(tags = {"商户-商品"})
@RestController
@RequestMapping("/shop/shop-goods")
public class ShopGoodsController {

    @Autowired
    private ShopGoodsService shopGoodsService;


    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Rest add(@RequestBody @Valid ShopGoodsAddForm form){
        shopGoodsService.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public Rest delete(@RequestParam Long id){
        shopGoodsService.delete(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("update")
    public Rest update(@RequestBody @Valid ShopGoodsUpdateForm form){
        shopGoodsService.update(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Rest<PageInfo<ShopGoodsVO>> page(@Valid ShopGoodsSearchForm form){
        IPage<ShopGoodsDTO> iPage = shopGoodsService.page(form);
        PageInfo<ShopGoodsVO> pageInfo = JsonUtil.pageTransform(iPage, ShopGoodsVO.class);

        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "详情")
    @GetMapping("get-by-id")
    public Rest<ShopGoodsVO> getById(@RequestParam Long id){
        ShopGoodsDTO shopGoodsDTO = shopGoodsService.getById(id);

        ShopGoodsVO shopGoodsVO = new ShopGoodsVO();
        BeanUtils.copyProperties(shopGoodsDTO, shopGoodsVO);

        return RestUtil.success(shopGoodsVO);
    }

}
